#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 35;
//f[i][j]表示从i到j结点建成子树能得到的最大分数
//g[i][j]表示从i到j结点简称的子树的根
//f[i][j] = max(w[k] + f[i][k - 1] * f[k + 1][j])
int f[N][N], g[N][N];
int w[N];
//f[i][j] = max(f[i][j], f[i][k - 1] * f[k + 1][j] + w[k]);
int n;

void print(int left, int right)
{
    if(left > right) return;
    int &v = g[left][right];
    cout << v << ' ';
    print(left, v - 1);
    print(v + 1, right);
    
}


int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> w[i];
    for(int len = 1; len <= n; ++len)
        for(int l = 1; l + len - 1 <= n; ++l)
        {
            int r = l + len - 1;
            if(l == r) f[l][l] = w[l], g[l][l] = l;
            else
            {
                for(int k = l; k <= r; ++k)
                {
                    int left = k == l ? 1 : f[l][k - 1];
                    int right = k == r ? 1 : f[k + 1][r];
                    if(left * right + w[k] > f[l][r])
                    {
                        g[l][r] = k;
                        f[l][r] = left * right + w[k];
                    }
                }
            }
        }
    cout << f[1][n] << endl;
    //打印方案
    print(1, n);
    return 0;
}